﻿<%@ WebHandler Language="C#" Class="PageUploadPageSelectHandler" %>

using System;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using System.Web.Security;
using System.Runtime.Remoting.Contexts;
using System.Data;
using Newtonsoft.Json;


public class PageUploadPageSelectHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string SelectAction = context.Request.QueryString["SelectAction"];//判斷要求執行的動作
        string strConn = global::LinkToDatabase.strConn;//宣告連線字串
        DataSet ds = new DataSet();//建立Dataset
        string strCmd;
        switch(SelectAction)
        {
            case "Begin":
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    strCmd = "select * from category";
                    using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
                    {
                        da.Fill(ds, "category");
                    }
                    string strCmd2 = "select * from brand";
                    using (SqlDataAdapter da = new SqlDataAdapter(strCmd2, conn))
                    {
                        da.Fill(ds, "brand");
                    }
                }
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(ds));
                return;
            case "Selecting":
                string Cid = context.Request.QueryString["Cid"];
                string Bid = context.Request.QueryString["Bid"];
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    strCmd = "select product_id,product_name from production_product where category_id=@Cid and brand_id=@Bid";
                    using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
                    {
                        da.SelectCommand.Parameters.AddWithValue("@Cid", Cid);
                        da.SelectCommand.Parameters.AddWithValue("@Bid", Bid);
                        da.Fill(ds, "product");
                    }
                }
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(ds));
                return;
            case "EditLoad":
                string Pid = context.Request.QueryString["Pid"];
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    strCmd = "select category_id,brand_id from production_product where product_id=@Pid";
                    using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
                    {
                        da.SelectCommand.Parameters.AddWithValue("@Pid", Pid);
                        da.Fill(ds, "product");
                    }
                }
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(ds));
                return;
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}